
var vm = new Vue({
	el: '#app',
	
	data:{
		SIGNUP_ID: '',		//主键ID
		pd: [],				//存放字段参数
		TESTPAPER_ID:'',	//试卷列表
		EXAMINE:false,		//是否需要审核
		STATE:true,			//报名状态
		msg:'add'
    },
	
	methods: {
		
        //初始执行
        init() {
        	var FID = this.getUrlKey('FID');	//当接收过来的FID不为null时,表示此页面是修改进来的
        	if(null != FID){
        		this.msg = 'edit';
        		this.SIGNUP_ID = FID;
        		this.getData();
        	}
        	setTimeout(function(){
        		vm.getTest();
            },200);
        },
        
        //去保存
    	save: function (){
    		if(this.msg == 'add'){
        		this.TESTPAPER_ID = $("#TESTPAPER_ID").val()+'';
        		if('null' == this.TESTPAPER_ID)this.TESTPAPER_ID = '';
				if(this.TESTPAPER_ID == '' || this.TESTPAPER_ID == undefined){
					$("#wfsj").tips({
						side:3,
			            msg:'请选择试卷',
			            bg:'#AE81FF',
			            time:2
			        });
					this.TESTPAPER_ID = '';
					this.$refs.TESTPAPER_ID.focus();
				return false;
				}
    		}
			if(this.pd.KSTITLE == '' || this.pd.KSTITLE == undefined){
				$("#KSTITLE").tips({
					side:3,
		            msg:'请输入考试主题',
		            bg:'#AE81FF',
		            time:2
		        });
				this.pd.KSTITLE = '';
				this.$refs.KSTITLE.focus();
			return false;
			}
			if(this.pd.KDNAME == '' || this.pd.KDNAME == undefined){
				$("#KDNAME").tips({
					side:3,
		            msg:'请输入考点名称',
		            bg:'#AE81FF',
		            time:2
		        });
				this.pd.KDNAME = '';
				this.$refs.KDNAME.focus();
			return false;
			}
			if(this.pd.ADDRESS == '' || this.pd.ADDRESS == undefined){
				$("#ADDRESS").tips({
					side:3,
		            msg:'请输入考点地址',
		            bg:'#AE81FF',
		            time:2
		        });
				this.pd.ADDRESS = '';
				this.$refs.ADDRESS.focus();
			return false;
			}
			if(this.pd.MAJOR == '' || this.pd.MAJOR == undefined){
				$("#MAJOR").tips({
					side:3,
		            msg:'请输入报考专业',
		            bg:'#AE81FF',
		            time:2
		        });
				this.pd.MAJOR = '';
				this.$refs.MAJOR.focus();
			return false;
			}
			if(this.pd.FEXPLAIN == '' || this.pd.FEXPLAIN == undefined){
				$("#FEXPLAIN").tips({
					side:3,
		            msg:'请输入考试说明',
		            bg:'#AE81FF',
		            time:2
		        });
				this.pd.FEXPLAIN = '';
				this.$refs.FEXPLAIN.focus();
			return false;
			}
			if(this.pd.MATTER == '' || this.pd.MATTER == undefined){
				$("#MATTER").tips({
					side:3,
		            msg:'请输入注意事项',
		            bg:'#AE81FF',
		            time:2
		        });
				this.pd.MATTER = '';
				this.$refs.MATTER.focus();
			return false;
			}
			if(this.pd.FRULE == '' || this.pd.FRULE == undefined){
				$("#FRULE").tips({
					side:3,
		            msg:'请输入考场规则',
		            bg:'#AE81FF',
		            time:2
		        });
				this.pd.FRULE = '';
				this.$refs.FRULE.focus();
			return false;
			}
    		
    		$("#showform").hide();
    		$("#jiazai").show();
    		
            //发送 post 请求提交保存
            $.ajax({
	            	xhrFields: {
	                    withCredentials: true
	                },
					type: "POST",
					url: httpurl+'signup/'+this.msg,
			    	data: {SIGNUP_ID:this.SIGNUP_ID,
				    TESTPAPER_ID:this.TESTPAPER_ID,
				    KSTITLE:this.pd.KSTITLE,
				    KDNAME:this.pd.KDNAME,
				    ADDRESS:this.pd.ADDRESS,
				    MAJOR:this.pd.MAJOR,
				    FEXPLAIN:this.pd.FEXPLAIN,
				    MATTER:this.pd.MATTER,
				    FRULE:this.pd.FRULE,
				    EXAMINE:this.EXAMINE,
				    STATE:this.STATE,
			    	tm:new Date().getTime()},
					dataType:"json",
					success: function(data){
                        if("success" == data.result){
                        	swal("", "保存成功", "success");
                        	setTimeout(function(){
                        		top.Dialog.close();//关闭弹窗
                            },1000);
                        }else if ("exception" == data.result){
                        	showException("报名管理",data.exception);//显示异常
                        	$("#showform").show();
                    		$("#jiazai").hide();
                        }
                    }
				}).done().fail(function(){
                   swal("登录失效!", "请求服务器无响应，稍后再试", "warning");
                   $("#showform").show();
           		   $("#jiazai").hide();
                });
    	},
    	
    	//根据主键ID获取数据
    	getData: function(){
    		//发送 post 请求
            $.ajax({
            	xhrFields: {
                    withCredentials: true
                },
				type: "POST",
				url: httpurl+'signup/goEdit',
		    	data: {SIGNUP_ID:this.SIGNUP_ID,tm:new Date().getTime()},
				dataType:"json",
				success: function(data){
                     if("success" == data.result){
                     	vm.pd = data.pd;									//参数map
                     	vm.EXAMINE = data.pd.EXAMINE == 'true'?true:false;	//是否需要审核
                     	vm.STATE = data.pd.STATE == 'true'?true:false;		//报名状态
                     }else if ("exception" == data.result){
                     	showException("报名管理",data.exception);	//显示异常
                     	$("#showform").show();
                 		$("#jiazai").hide();
                     }
                  }
			}).done().fail(function(){
                  swal("登录失效!", "请求服务器无响应，稍后再试", "warning");
                  $("#showform").show();
          		  $("#jiazai").hide();
               });
    	},
    	
    	//调用试卷列表(未发放的试卷)
    	getTest: function(){
    		$.ajax({
    			xhrFields: {
                    withCredentials: true
                },
    			type: "POST",
    			url: httpurl+'testpaper/nograntList',
    	    	data: {tm:new Date().getTime()},
    			dataType:'json',
    			success: function(data){
    				$("#TESTPAPER_ID").html('<option value="" >要选择的试卷</option>');
    				 $.each(data.varList, function(i, dvar){
    					 $("#TESTPAPER_ID").append("<option value="+dvar.TESTPAPER_ID+">"+dvar.TITLE.substring(0,14)+"...</option>");
    				 });
    			}
    		});
    	},
    	
    	//根据url参数名称获取参数值
        getUrlKey: function (name) {
            return decodeURIComponent(
                (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
        }
        
	},
	
	mounted(){
        this.init();
    }
})